package form

import (
	"errors"
	"yak.admin/src/entity"
	"yak.admin/src/util"
)

type UserCheckForm struct {
	Id       int `form:"id" validate:"required" chn:"用户ID"`
	Province string
	City     string
	County   string
	IsSuper  bool
}

func (form *UserCheckForm) Checked() error {
	db := util.GetMysqlPool().Handler()
	user := entity.User{}
	var condition map[string]interface{}
	if form.IsSuper {
		condition = map[string]interface{}{"id": form.Id, "status": util.UserStatusReady}
	} else {
		condition = map[string]interface{}{"id": form.Id, "status": util.UserStatusReady, "province": form.Province, "city": form.City, "county": form.County}
	}

	if !db.Where(condition).First(&user).RecordNotFound() {
		if db.Model(&user).Updates(map[string]interface{}{
			 "status": util.UserStatusOK,
		}).RowsAffected > 0 {
			return nil
		}
	}

	return errors.New("用户已激活")
}


func (form *UserCheckForm) Del() error {
	db := util.GetMysqlPool().Handler()
	user := entity.User{}
	var condition map[string]interface{}
	if form.IsSuper {
		condition = map[string]interface{}{"id": form.Id, "status": util.UserStatusReady}
	} else {
		condition = map[string]interface{}{"id": form.Id, "status": util.UserStatusReady, "province": form.Province, "city": form.City, "county": form.County}
	}

	if !db.Where(condition).First(&user).RecordNotFound() {
		if db.Delete(&user).RowsAffected > 0 {
			return nil
		}
	}

	return errors.New("激活中用户禁止删除")
}


